package stark.app.function.cartoon;

import android.graphics.Bitmap;
import android.net.Uri;
import android.widget.SeekBar;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.picselect.PhotoSelectorBuilder;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.interfaces.OnSelectResultListener;

import java.util.List;

import io.reactivex.rxjava3.core.ObservableEmitter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageToonFilter;
import stark.app.R;
import stark.app.databinding.FragmentCartoonImgBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.RxUtil;

/**
 * Author：daizhongbin on 2023/4/27
 * Email：1027154338@qq.com
 * Desc: 该界面中使用到了GPUImageView，其对应的依赖为implementation rootProject.ext.library.gpuimage，所以请各位开发时务必在项目中添加该依赖
 */
public class CartoonImgFragment extends BaseNoModelFragment<FragmentCartoonImgBinding> {

    @Override
    protected int onCreate() {
        return R.layout.fragment_cartoon_img;
    }

    @Override
    protected void initView() {
        mDataBinding.btnSelImg.setOnClickListener(v -> {
            selImg();
        });
        mDataBinding.btnSaveImg.setOnClickListener(v -> {
            saveImg();
        });
        mDataBinding.sbProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                GPUImageToonFilter filter = (GPUImageToonFilter) mDataBinding.gpuImgView.getFilter();
                filter.setLineSize(calLineSize(progress));
                mDataBinding.gpuImgView.requestRender();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        mDataBinding.btnSelImg.performClick();
    }

    @Override
    protected void initData() {

    }

    private float calLineSize(int progress) {
        float start = 0f;
        float end = 5.0f;
        float ret = (end - start) * progress / 100f + start;
        return ret;
    }

    private void selImg() {
        PhotoSelectorBuilder.builder(getActivity())
                .mode(PhotoConfig.Mode.PHOTO)
                .maxCount(1)
                .listener(new OnSelectResultListener<List<SelectMediaEntity>>() {
                    @Override
                    public void onResult(List<SelectMediaEntity> data) {
                        if (data == null || data.size() == 0) {
                            return;
                        }
                        String uriStr = data.get(0).getUri();
                        mDataBinding.gpuImgView.setImage(Uri.parse(uriStr));
                        mDataBinding.gpuImgView.setFilter(new GPUImageToonFilter());
                    }
                });
    }

    private void saveImg() {
        showDialog("保存中...");
        RxUtil.create(new RxUtil.Callback<Uri>() {
            @Override
            public void doBackground(ObservableEmitter<Uri> emitter) {
                Uri uri = null;
                try {
                    Bitmap bitmap = mDataBinding.gpuImgView.capture();
                    uri = FileP2pUtil.saveBitmap2Jpg(getContext(), bitmap);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                emitter.onNext(uri);
            }

            @Override
            public void accept(Uri uri) {
                dismissDialog();
                ToastUtils.showShort(uri == null ? "保存失败" : "保存成功，请到系统相册中查看");
            }
        });
    }
}
